from fastapi import APIRouter,Response
from fastapi.responses import JSONResponse

router = APIRouter()

@router.get("/cookie_and_object")
async def create_cookie(response: Response):
    response.set_cookie(key="session_id", value="abc123")
    return {"msg": "这是响应的主体内容"}

@router.post("/cookie")
async def create_cookie2():
    content = {"message": "这是响应的主体内容"}
    response = JSONResponse(content=content)
    # 在响应中设置cookie
    response.set_cookie(key="session_id", value="abc123")
    return response